package main

import (
	"fmt"
	"math"
)

// 计算最优充电和放电时间区域
func findOptimalChargeDischargePeriods(prices []float64, loads []float64, chargeRequirement float64, dischargeRequirement float64, maxLoad float64) (int, int, int, int) {
	n := len(prices)

	// 初始化结果
	minCost := math.MaxFloat64
	chargeStart, chargeEnd := 0, 0
	dischargeStart, dischargeEnd := 0, 0

	// 遍历所有可能的时间区间
	for i := 0; i < n; i++ {
		for j := i; j < n; j++ {
			totalLoad := 0.0
			totalCharge := 0.0
			totalDischarge := 0.0
			totalPrice := 0.0

			// 计算当前时间区间的总负载、总充电量、总放电量和总价格
			for k := i; k <= j; k++ {
				totalLoad += loads[k]
				totalPrice += prices[k]
				if loads[k] > 0 {
					totalCharge += loads[k]
				} else {
					totalDischarge -= loads[k]
				}
			}
			// 检查是否满足最大负载限制和充放电需求
			if totalLoad <= maxLoad {
				// 优化充电时间区间
				if totalCharge >= chargeRequirement {
					if totalPrice < minCost {
						minCost = totalPrice
						chargeStart, chargeEnd = i, j
					}
				}
				// 优化放电时间区间
				if totalDischarge >= dischargeRequirement {
					if totalPrice < minCost {
						minCost = totalPrice
						dischargeStart, dischargeEnd = i, j
					}
				}
			}
		}
	}
	return chargeStart, chargeEnd, dischargeStart, dischargeEnd
}

func main() {
	// 示例数据
	prices := []float64{0.2, 0.3, 0.5, 0.4, 0.6}
	loads := []float64{5, -10, 7, -8, 6} // 正数为充电，负数为放电
	chargeRequirement := 10.0
	dischargeRequirement := 8.0
	maxLoad := 20.0

	chargeStart, chargeEnd, dischargeStart, dischargeEnd := findOptimalChargeDischargePeriods(prices, loads, chargeRequirement, dischargeRequirement, maxLoad)
	fmt.Printf("最优充电时间区间是: 从小时 %d 到 %d\n", chargeStart, chargeEnd)
	fmt.Printf("最优放电时间区间是: 从小时 %d 到 %d\n", dischargeStart, dischargeEnd)
}
